//
//  BrowserViewController.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/5/13.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "BrowserViewController.h"
#import <MBProgressHUD.h>
#import "CustomActivity.h"

@interface BrowserViewController() {
    MBProgressHUD *HUD;
}

@end

@interface BrowserViewController ()

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation BrowserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupView];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSString* webStringURL = [self.urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:webStringURL]];
    [MBProgressHUD showMessage:@"数据加载中..." toView:self.view];
    [self.webView loadRequest:request];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupView {

    UIView *actionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, kNavigation_Bar_Height)];
    
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    [shareBtn setTintColor:[UIColor whiteColor]];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize: kListTitleFontSize];
    shareBtn.frame = CGRectMake(0, 0, [Tools sizeOfSingleLineText:@"分享" withSize:kListTitleFontSize].width + 5, [Tools sizeOfSingleLineText:@"分享" withSize:kListTitleFontSize].height + 5);
    [shareBtn addTarget:self action:@selector(shareButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [actionView addSubview:shareBtn];
    shareBtn.centerY = kNavigation_Bar_Height/2;
    
    actionView.width = CGRectGetMaxX(shareBtn.frame);
    
    //自定义按钮
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:actionView];
    self.navigationItem.rightBarButtonItem = backBtn;
    
    self.webView = [[UIWebView alloc] initWithFrame:[UIView frameWithOutNav]];
    self.webView.scalesPageToFit = YES;
    [self.view addSubview: self.webView];
    self.webView.delegate = self;
    //[self.webView loadRequest:request];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUDForView:self.view];
}

- (void)shareButtonClicked {
    NSString *shareText = @"产品文档";
    UIImage *shareImage = [UIImage imageNamed:@"share_icon"];
    NSString* webStringURL = [self.urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *shareUrl = [NSURL URLWithString:webStringURL];
    NSArray *activityItemsArray = @[shareText,shareImage,shareUrl];
    
    // 自定义的CustomActivity，继承自UIActivity
    CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle:shareText ActivityImage:[UIImage imageNamed:@"share_icon"] URL:shareUrl ActivityType:@"Custom"];
    //    CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle: shareText ActivityImage:[UIImage imageNamed:@"custom.png"] URL: shareUrl ActivityType:@"Custom" ShareContext:activityItemsArray];
    NSArray *activityArray = @[customActivity];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:activityArray];
    activityVC.modalInPopover = YES;
    
    UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        NSLog(@"activityType == %@",activityType);
        if (completed == YES) {
            NSLog(@"completed");
        }else{
            NSLog(@"cancel");
        }
    };
    activityVC.completionWithItemsHandler = itemsBlock;
    
    [self presentViewController:activityVC animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
